﻿using System.Collections;
using System.Collections.Generic;
using Components;
using Const;
using Entitas;
using UnityEngine;

public class JudgeFormationSystem : ReactiveSystem<GameEntity>
{
    public JudgeFormationSystem(Contexts context) : base(context.game)
    {
    }

    protected override ICollector<GameEntity> GetTrigger(IContext<GameEntity> context)
    {
        return context.CreateCollector(GameMatcher.ComponentsJudgeFormation);
    }

    protected override bool Filter(GameEntity entity)
    {

        return entity.isComponentsJudgeFormation;
        //&& entity.componentsItemEffectState.EffectState == ItemEffectName.NONE;
    }

    protected override void Execute(List<GameEntity> entities)
    {
        foreach (GameEntity entity in entities)
        {
            if (entity.componentsItemEffectState.EffectState == ItemEffectName.NONE)
            {
               
                //开始判断阵型
                JudgeItem(entity);
            }
            else
            {
                entity.isComponentsJudgeFormation = false;
            }
            entity.ReplaceComponentsEliminate(true);
        }
    }

    private void JudgeItem(GameEntity entity)
    {
        //用来修改ItemEffectName的状态的
        if (JudgeAllSameColor(entity.componentsDetecteSameItem))
        {
            entity.ReplaceComponentsItemEffectState(ItemEffectName.ELIMINATE_SAME_COLOR);
           // Debug.Log("SameColor!!!");
        }
        else if (JudgeExplode(entity.componentsDetecteSameItem))
        {
            entity.ReplaceComponentsItemEffectState(ItemEffectName.EXPLODE);  
            //Debug.Log("Explode!!!");
        }
        else if(JudgeHorizontal(entity.componentsDetecteSameItem))
        {
            entity.ReplaceComponentsItemEffectState(ItemEffectName.ELIMINATE_HORIZONTAL);
           // Debug.Log("Horizontal!!!");
        }
        else if(JudgeVertical(entity.componentsDetecteSameItem))
        {
            entity.ReplaceComponentsItemEffectState(ItemEffectName.ELIMINATE_VERTICAL);
           // Debug.Log("Vertical!!!");
        }
        else
        {
            entity.isComponentsJudgeFormation = false;
        }
    }

    //判断消除一整列
    private bool JudgeVertical(DetecteSameItem listComponent)
    {
        if (listComponent.UpEntities.Count + listComponent.DownEntities.Count == 3)
        {
            return true;
        }

        return false;
    }
    //判断消除一整行
    private bool JudgeHorizontal(DetecteSameItem listComponent)
    {
        if (listComponent.LeftEntities.Count + listComponent.RightEntities.Count == 3)
        {
            return true; 
        }
        return false;
    }
    //判断消除所有颜色
    private bool JudgeAllSameColor(DetecteSameItem listComponent)
    {
        if (listComponent.UpEntities.Count + listComponent.DownEntities.Count >= 4
            ||listComponent.LeftEntities.Count + listComponent.RightEntities.Count >= 4)
        {
            return true;
        }

        return false;
    }
    
    //判断爆炸效果（消除九宫格）
    private bool JudgeExplode(DetecteSameItem listComponent)
    {
        int vertical = listComponent.UpEntities.Count + listComponent.DownEntities.Count;
        int horizontal = listComponent.LeftEntities.Count + listComponent.RightEntities.Count;
        
        if (vertical>=2&&horizontal>=2)
        {
            return true;
        }
        return false;
    }
    
    
    
}
